<?php
    header("Content-type:text/html;charset=utf-8");
    // 1.定义一个返回内容
    $resData = array(
        "code"=>0,
        "message" => "啥也没有"
    );

    // 2.拿到数据
    $username = $_POST['username'];
    $password = $_POST['password'];

    // 3.判断用户名和密码是否为空
    if(!$username){
        $resData["code"] = 1;
        $resData["message"] = "用户名不能为空";
        echo json_encode($resData);
        exit;
    }
    if(!$password){
        $resData["code"] = 2;
        $resData["message"] = "密码不能为空";
        echo json_encode($resData);
        exit;
    }

    // 4.数据库连接
    $link = mysqli_connect("localhost","root","root");
    mysqli_set_charset($link,"utf-8");
    mysqli_select_db($link,"login2");
        // 4-1判断数据库是否连接成功
        if(!$link){
            $resData["code"] = 3;
            $resData["message"] = "数据库连接失败";
            echo ($resData);
            exit;
        }
    
    // 5.密码加密
    $str = md5(md5(md5($password).'dajia').'china');
    
    // 6.进行数据库查询
    $sql = "SELECT * FROM user WHERE username = '{$username}' AND password = '{$str}' ";
    $res = mysqli_query($link,$sql);
    $row = mysqli_fetch_assoc($res); 
    if(!$row){
        $resData["code"] = 4;
        $resData["message"] = "用户名或密码错误";
        echo json_encode($resData);
        exit;
    }else{
        $resData["message"] = "登录成功！";
        echo json_encode($resData);
    }

    // 7.关闭数据库
    mysqli_close($link);

?>